//
//  NewsCell.swift
//  Jock
//
//  Created by HD on 15/3/14.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

class NewsCell: UITableViewCell {
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var nickNameView: UILabel!
    @IBOutlet weak var contentLable: UILabel!
    @IBOutlet weak var contentImageView: UIImageView!
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var likeLabel: UILabel!
    @IBOutlet weak var unlikeLabel: UILabel!
    @IBOutlet weak var commentLabel: UILabel!
    
    var news: News?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    func showData() {
        if news != nil {
            if let user = news?.user {
                nickNameView.text = user.login
                if user.icon != nil {
                    var userIcon = user.icon
                    var userId =  user.id as NSString
                    var prefixUserId = userId.substringToIndex(4)
                    var url = "http://pic.qiushibaike.com/system/avtnew/\(prefixUserId)/\(userId)/thumb/\(userIcon)"
                    ImageLoader.loadImage(url, imageCacheDir: "iconCache", placeHolderName: "avatar.jpg", loadImageView: iconView)                    
                }
            } else {
                nickNameView.text = "匿名"
                iconView.image = UIImage(named: "avatar.jpg")
            }
            contentLable.text = news?.content
            if let imageString = news?.image {
                var imageId = news!.id as NSString
                var prefiximageId = imageId.substringToIndex(5)
                var imagURL = "http://pic.qiushibaike.com/system/pictures/\(prefiximageId)/\(imageId)/small/\(imageString)"
                self.contentImageView.hidden = false
                ImageLoader.loadImage(imagURL, imageCacheDir: "headImageCache", placeHolderName: "avatar.jpg" , callBack: { (image) -> Void in
                    let height = image.size.height * self.contentImageView.bounds.width / image.size.width
                    let size = CGSize(width: self.contentImageView.bounds.width, height: height)
                    var newImage = image.scaleToSize(size)
                    self.contentImageView.image = newImage
                    self.contentImageView.setHeight(height)
                })
            } else {
                contentImageView.hidden = true
            }
            likeLabel.text = "赞(\(news!.votesUp))"
            unlikeLabel.text = "踩(\(news!.votesDown))"
            commentLabel.text = "评论(\(news!.comments_count))"
        }
    }

}
